package main

import (
	"encoding/json"
	"fmt"
	"regexp"
	"strings"
	"time"

	"github.com/gogf/gf/os/gtime"

	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/encoding/gurl"
	gjson1 "github.com/tidwall/gjson"
)

type S struct {
	Ext map[string]string `json:"ext"`
}

func main() {
	a := make(map[string]string)
	res := S{Ext: a}

	//res := d()

	fmt.Printf("res = %+v\n", res)
	bytes, _ := json.Marshal(res)
	fmt.Println(string(bytes))

	parseUrl()

	replaceT()

	testGetAttr()

	ddd()

	e()

	regular()

	aaaa()

	tGtime()

	//tJson()

	tSlice()

	tBar()

	t1()

	t2()
}

func d() (res S) {
	return
}

func parseUrl() {
	urlStr := `shihuo://www.shihuo.cn?route=go&goods_product_id=8908344&tpExtra=%7B%22fReqId%22%3A%22178961FBA85D7F5DB6569F6F25A2FEA7%22%2C%22fTp%22%3A%22searchList%3Alist%3A1%22%2C%22fTpName%22%3A%22%22%7D&url=https%3A%2F%2Fitem.taobao.com%2Fitem.htm%3Fid%3D627199822018%26skuId%3D4445745408253%26dspm%3D21feffe679840fdf_db6f30c41210a604%26fromShType%3D1%26goodsType%3D7%26openType%3D1%26shopId%3Dcn.taobao.105738637#%7B%22from%22%3A%22shihuo%3A%2F%2Fwww.shihuo.cn%3Froute%3DgoodsDetail%22%2C%22block%22%3A%22Supplier%22%2C%22tp%22%3A%22Supplier%22%2C%22sku_id%22%3A49719950%2C%22extra%22%3A%222%22%2C%22dspm%22%3A%2221feffe679840fdf_db6f30c41210a604%22%2C%22goods_id%22%3A%22440938%22%2C%22supplier_id%22%3A%225238143%22%2C%22style_id%22%3A%224915937%22%2C%22size%22%3A%224G%2B64G%22%2C%22price%22%3A%22879.9%22%2C%22sort%22%3A%22price_a%22%2C%22filter%22%3A%220000001%22%2C%22all_nice_price%22%3A%22879%22%2C%22store_nice_price%22%3A%22879%22%2C%22tag_name%22%3A%22%E8%AF%86%E8%B4%A7%E6%9C%80%E4%BD%8E%E4%BB%B7%22%7D`
	data, err := gurl.ParseURL(urlStr, -1)
	if err != nil {
		panic(err)
	}
	fmt.Println(data["fragment"])

}

func replaceT() {
	r := strings.Replace("44.5 - oversea", " - oversea", "", -1)
	fmt.Println(r)
}

func testGetAttr() {
	items := `
        {
            "SkuId": 59402392,
            "Unique": "cn.jd.56664781393",
            "Attr": [
                {
                    "Nid": "1",
                    "N": "颜色",
                    "Vid": "1000005",
                    "V": "紫色（微瑕）"
                },
                {
                    "Nid": "2",
                    "N": "版本",
                    "Vid": "2000004",
                    "V": "全网通 128GB【新包装】"
                }
            ],
            "Offers": [
                {
                    "Merchant": {
                        "Name": "jd"
                    },
                    "List": [
                        {
                            "StartAt": "0001-01-01 00:00:00",
                            "EndAt": "0001-01-01 00:00:00",
                            "Price": 4949,
                            "Type": "RMB",
                            "Text": "",
                            "ActivityType": null
                        }
                    ],
                    "Stock": null,
                    "Subtitle": ""
                }
            ]
        }
    `
	data := &SpiderDataItems{}
	err := json.Unmarshal([]byte(items), &data)
	if err != nil {
		panic(err)
	}

	attr := GetAttr(data, "颜色")
	fmt.Println("attr = ", attr)
}

func GetAttr(item *SpiderDataItems, specs string) (attr string) {
	attr = ""
	for _, v := range item.Attr {
		pattern, err := regexp.Compile("颜色")
		if err != nil || pattern == nil {
			continue
		}
		if pattern.MatchString(v.Specs) {
			attr = v.Attr
			attr = strings.Replace(attr, "'", "", -1)
			attr = strings.Replace(attr, `"`, "", -1)
			return
		}
	}
	return
}

type SpiderDataItems struct {
	SkuId  int64                `json:"SkuId"`
	Attr   []*SpiderDataSkuItem `json:"Attr"`
	Offers []*SpiderDataSkuOffer
}
type SpiderDataSkuItem struct {
	Specs string `json:"N"`
	Attr  string `json:"V"`
}
type SpiderDataSkuOffer struct {
	List []*SpiderDataSkuOfferListItem
}
type SpiderDataSkuOfferListItem struct {
	Price float64
	Type  string
	Text  string
}

func ddd() {
	a := "WLAN版128GB"
	b := "灰色"
	c := "WLAN版128GB版"
	d := "WLAN版128GB版ddd"

	as := strings.Split(a, "版")
	bs := strings.Split(b, "版")
	cs := strings.Split(c, "版")
	ds := strings.Split(d, "版")

	fmt.Printf("as = %+v,bs = %+v,cs = %+v,ds = %+v\n", as, bs, cs, ds)
}

func e() {
	var res string
	//bytes := `[91 53 52 44 32 52 44 32 49 51 44 32 49 54 44 32 50 48 44 32 50 51 44 32 52 49 93]`
	str := `"[54, 4, 13, 16, 20, 23, 41]"`
	err := json.Unmarshal([]byte(str), &res)
	if err != nil {
		panic(err)
	}

	fmt.Println("res = ", res)

	str1 := `[54, 4, 13, 16, 20, 23, 41]`
	b, _ := json.Marshal(str1)
	fmt.Println("b = ", b)

	c, _ := json.Marshal("[54, 4, 13, 16, 20, 23, 41, 10000]")
	fmt.Println("c = ", c)

	d := string([]byte{99, 110, 46, 116, 97, 111, 98, 97, 111, 46, 49, 48, 53, 53, 55, 51, 55, 52, 51})
	fmt.Println(d)
}

func regular() {
	pattern := "7天"
	match1, _ := regexp.MatchString(pattern, "7天可退（拆封后不可退）")
	fmt.Println(match1)
}

func aaaa() {
	a := `{
    "day": {
        "int": 28
    },
    "hour": {
        "int": 17
    },
    "millis": {
        "int": 0
    },
    "minute": {
        "int": 6
    },
    "month": {
        "int": 1
    },
    "second": {
        "int": 31
    },
    "year": {
        "int": 2021
    }
}`
	b := map[string]map[string]int{}
	err := json.Unmarshal([]byte(a), &b)
	if err != nil {
		panic(err)
	}

	fmt.Println(b)

	c := gjson.New(a)
	d := c.GetInt("day.int")
	c.GetInt64("day.int")
	fmt.Println(d)

	e := gjson1.Get(a, "day.int").Int()
	fmt.Println(e)
}

func tGtime() {
	a := gtime.NewFromStr("2021-01-29 14:50:00")
	fmt.Println("a1 = ", a)
	fmt.Println("a2 = ", a.Unix())

	b := gtime.Date() //获取当前年月日
	fmt.Println("b = ", b)

	c := gtime.Datetime() //获取当前年月日 时分秒
	fmt.Println("c = ", c)

	t, _ := time.Parse("2006-01-02 15:04:05", "2021-01-29 14:50:00")
	d := gtime.New(t)
	fmt.Println("d = ", d)

	e := gtime.Now()
	fmt.Println("e = ", e)

	f, _ := gtime.StrToTime("2021-01-29 14:50:00", "Y-m-d H:i:s")
	fmt.Println("f = ", f)
}

func tJson() {
	a := ""
	ids := []int64{}
	err := json.Unmarshal([]byte(a), &ids)
	if err != nil {
		panic(err)
	}
	fmt.Println(err)
}

type s struct {
	Name string `json:"name"`
}

func tSlice() {
	a := []*s{}
	fmt.Println(a)
}

type Data struct {
	Bar *Bar `json:"bar,omitempty"`
	Sex int  `json:"sex"`
}

type Bar struct {
	Name string `json:"name"`
}

func tBar() {
	a := &Data{
		Sex: 1,
	}
	bs, _ := json.Marshal(a)
	fmt.Println(string(bs))

	b := &Data{
		Bar: &Bar{Name: "12"},
		Sex: 0,
	}
	bs, _ = json.Marshal(b)
	fmt.Println(string(bs))
}

func t1() {
	a := &Data{
		Sex: 1,
	}
	bs, _ := json.Marshal(a)

	b := &Data{}
	err := json.Unmarshal(bs, &b)
	if err != nil {
		panic(err)
	}
	fmt.Println("b = ", b)
}

type ThirdResultTaoLiJinSupplier struct {
	Status int                                        `json:"status"`
	Msg    string                                     `json:"msg"`
	Data   map[int64]*ThirdResultTaoLiJinSupplierData `json:"data"`
}
type ThirdResultTaoLiJinSupplierData struct {
	Discount float64 `json:"discount"`
}

func t2() {
	bs := `{
    "status": 0,
    "msg": "",
    "data": {
        "3808474": {
            
        },
        "4071221": {
            
        },
        "4475236": {
            
        },
        "4870559": {
            
        },
        "5009885": {
            
        },
        "5238486": {
           
        },
        "5238883": {
            
        },
        "5239016": {
            
        },
        "5239314": {
            
        }
    }
}`
	data := &ThirdResultTaoLiJinSupplier{}
	_ = json.Unmarshal([]byte(bs), &data)
	fmt.Println(data)
}
